package c.x.jy.service.service.impl;

import c.x.jy.service.redis.Cache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import c.x.jy.common.utils.PageUtils;
import c.x.jy.common.utils.Query;

import c.x.jy.dao.dao.SysRoleMenuDao;
import c.x.jy.dao.entity.SysRoleMenuEntity;
import c.x.jy.service.service.SysRoleMenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;


@Service("sysRoleMenuService")
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuDao, SysRoleMenuEntity> implements SysRoleMenuService {


    @Autowired
    private Cache redisUtils;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        	QueryWrapper<SysRoleMenuEntity> query= 	 new QueryWrapper<SysRoleMenuEntity>() ;
    	if(params.get("searchKey")!=null&&params.get("searchValue")!=null&&!StringUtils.isEmpty(params.get("searchKey").toString())&&!StringUtils.isEmpty(params.get("searchValue").toString()))
    	    query.eq(  params.get("searchKey").toString(), params.get("searchValue"));
        if(params.get("businessId")!=null) 	query.eq("business_id", params.get("businessId"));
        IPage<SysRoleMenuEntity> page = this.page(
                new Query<SysRoleMenuEntity>().getPage(params),
                 query
        );

        return new PageUtils(page);
    }


    /**
     *
     */
    @Transactional
    @Override
    public void granted(List menuIds, Long roleId) {
        QueryWrapper<SysRoleMenuEntity> wrapper=new QueryWrapper<>();
        wrapper.lambda().eq(SysRoleMenuEntity::getRoleId,roleId) ;//.eq("role_id", roleId);
        this.baseMapper.delete(wrapper);
        List<SysRoleMenuEntity> list=new ArrayList<>();
        for (Object menuId : menuIds) {
            if(menuId==null)continue;
            SysRoleMenuEntity sr=new SysRoleMenuEntity();
            sr.setMenuId(Long.valueOf( menuId.toString()));
            sr.setRoleId(roleId);
           list.add(sr);
        }
        this.baseMapper.insertbatch(list);
        redisUtils.remove("ShiroServiceImpl:getUserPermissions:");
    }

}
